/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.NotificationHandler;
import com.comut.crud.bo.TrailerHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.ITrailerActionLocal;
import com.comut.crud.ejb.interfaces.remote.ITrailerActionRemote;
import com.comut.crud.pojo.mobile.TrailerDetail;
import com.comut.crud.pojo.mobile.TrailerInfo;
import com.comut.crud.pojo.trailer.Trailer;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
//@TransactionManagement(TransactionManagementType.BEAN)
//@WebContext(contextRoot = "/CRUD-EA-war/resources/Trailer")
//@WebService(endpointInterface = "com.comut.ejb.interfaces.ITrailerAction", targetNamespace = "http://ws.crud.comut.com/")
public class TrailerBean implements ITrailerActionLocal, ITrailerActionRemote {

    private static Logger _logger = Logger.getLogger(TrailerBean.class.getName());

    @PostConstruct
    private void trailerPostConstruct() {
        _logger.info("trailerPostConstruct");
    }

    @PreDestroy
    private void trailerPreDestroy() {
        _logger.info("PreDestroy");
    }

    public TrailerBean() {
    }

    public Trailer insertTrailer(Trailer trailer) {
        Connection connection = null;
        Trailer toRet = null;
        TrailerHandler th = null;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new TrailerHandler(connection);
            toRet = th.insertTrailer(trailer);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutTrailers();
        return toRet;
    }

    public void deleteTrailer(Trailer trailer) {
        if(trailer.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);

        Connection connection = null;
        TrailerHandler th = null;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new TrailerHandler(connection);
            th.deleteTrailer(trailer);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutTrailers();
    }

    public void updateTrailer(String trailer) {
        Connection connection = null;
        TrailerHandler th = null;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new TrailerHandler(connection);
            th.updateTrailer(trailer);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutTrailers();
    }

    /**
     *  @author celia calcada
     *  ...
     */
    public Trailer getTrailer(Trailer trailer) {
        Connection connection = null;
        Trailer toRet = null;
        TrailerHandler th = null;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new TrailerHandler(connection);
            toRet = th.getTrailer(trailer);

        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }

    /**
     * Issue: IFL-1572
     * @author celia calcada
     * @param userId
     * @return
     */
    public TrailerInfo[] getTrailersForUserId(int userId) {
        Connection connection = null;
        TrailerInfo[] ret = null;

        TrailerHandler trailerHandler = null;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            trailerHandler = new TrailerHandler(connection);
            ret = trailerHandler.getTrailersForUserId(userId);

        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            trailerHandler = null;
        }

        return ret;
    }

    /**
     * Issue: IFL-1573
     * @author celia calcada
     * @param userId
     * @param trailerId
     * @return
     */
    public TrailerDetail getTrailerDetail(int userId, int trailerId) {
        Connection connection = null;
        TrailerDetail ret = null;

        TrailerHandler trailerHandler = null;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            trailerHandler = new TrailerHandler(connection);
            ret = trailerHandler.getTrailerDetail(userId, trailerId);

        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            trailerHandler = null;
        }

        return ret;
    }
}
